package com.baozun.util.items;
import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspWriter;


/**
 * 选择项的集合,来自于管理通
 * 
 * @author Forrest
 */
public class SelectSet {

	private static SelectSet ss = new SelectSet("");
	/**
	 * 各字典表解析
	 */
	private static List<Item> urgentLevel;//优先级
	private static List<Item> bloodType;//血型
	private static List<Item> finishLevel;//完成情况
	private static List<Item> sex;//性别
	private static List<Item> workrecordType;//工作日志类型
	/**
	 * 是否
	 */
	private static List<Item> yesOrNo;//是否
	/**
	 * 
	 * @return
	 */
	public static SelectSet getSs() {
		return ss;
	}

	public static void setSs(SelectSet ss) {
		SelectSet.ss = ss;
	}

	/**
	 * 初始化选择项集合
	 * 
	 * @param filePath
	 */
	private SelectSet(String filePath) {
		ParseSelectXmlUtil parseSelectXmlUtil = new ParseSelectXmlUtil();
		urgentLevel = parseSelectXmlUtil.getSelectItems("URGENT_LEVEL.xml");//优先级
		bloodType = parseSelectXmlUtil.getSelectItems("BLOOD_TYPE.xml");//血型
		finishLevel = parseSelectXmlUtil.getSelectItems("FINISH_LEVEL.xml");//完成情况
		yesOrNo = parseSelectXmlUtil.getSelectItems("YES_OR_NO.xml");//是否
		sex = parseSelectXmlUtil.getSelectItems("SEX.xml");//性别
		workrecordType = parseSelectXmlUtil.getSelectItems("WORKRECORD_TYPE.xml");//工作日志类型
	}

	/**
	 * 输入，根据ID得到字典表对应项下拉框
	 * 
	 * @param itemList
	 * @param out
	 * @param id
	 * @throws IOException
	 *             example:
	 *             SelectSet.outputOptions(SelectSet.getBloodType(),out,
	 *             "1")</br> <option value="1" selected>A型</option></br> <option
	 *             value="2">B型</option></br> <option
	 *             value="3">AB型</option></br> <option
	 *             value="4">O型</option></br> <option value="5">其他型</option>
	 */
	public static void outputOptions(List<Item> itemList, JspWriter out, String id) throws IOException {
		for (Item item : itemList) {
			out.print("<option value=\"" + item.getId() + "\"" + (item.getId().equals(id) ? " selected" : "") + ">");
			out.print(item.getCaption());
			out.print("</option>");
		}
	}

	/**
	 * 输入，根据ID得到字典表对应项下拉框
	 * 
	 * @param itemList
	 * @param out
	 * @param id
	 * @throws IOException
	 *             example:
	 *             SelectSet.outputOptions(SelectSet.getBloodType(),out,
	 *             "1")</br> <option value="1" selected>A型</option></br> <option
	 *             value="2">B型</option></br> <option
	 *             value="3">AB型</option></br> <option
	 *             value="4">O型</option></br> <option value="5">其他型</option>
	 */
	public static void outputOptions(List<Item> itemList, JspWriter out) throws IOException {
		for (Item item : itemList) {
			out.print("<option value=\"" + item.getId() + "\"" + ">");
			out.print(item.getCaption());
			out.print("</option>");
		}
	}

	/**
	 * 得到<option>这样的字符串，用于ajax使用
	 * 
	 * @param itemList
	 * @return
	 */
	public static String outputStr(List<Item> itemList) {
		StringBuffer sb = new StringBuffer();
		for (Item item : itemList) {
			sb.append("<option value=\"" + item.getId() + "\"" + ">");
			sb.append(item.getCaption());
			sb.append("</option>");
		}
		return sb.toString();
	}

	/**
	 * 根据ID得到对应的显示名称
	 * 
	 * @param List
	 *            <Item>itemList
	 * @param String
	 *            id example
	 *            SelectSet.outPutString(SelectSet.getBloodType(),"1")="A型"
	 */
	public static String outPutString(List<Item> itemList, String id) throws IOException {
		String result = "";
		for (Item item : itemList) {
			if ( item.getId().equals(id) ) {
				result = item.getCaption();
				break;
			}
		}
		return result;
	}

	/**
	 * 返回选择项集合
	 * 
	 * @param filePath
	 * @return
	 */
	public static SelectSet getInstanse() {
		return ss;
	}

	/**
	 * 根据Caption得到一个List中的Item
	 * 
	 * @param list
	 * @param name
	 * @return
	 */
	public static Item getItemByCaption(List<Item> list, String name) {
		if ( list != null && list.size() > 0 ) {
			for (Item item : list) {
				if ( item.getCaption().equals(name) ) {
					return item;
				}
			}
		}

		return null;
	}

	/**
	 * 根据ID得到一个List中的Item
	 * 
	 * @param list
	 * @param name
	 * @return
	 */
	public static Item getItemById(List<Item> list, String name) {
		if ( list != null && list.size() > 0 ) {
			for (Item item : list) {
				if ( item.getId().equals(name) ) {
					return item;
				}
			}
		}
		return null;
	}

	/**
	 * 根据ID得到一个List中的Caption
	 * 
	 * @param list
	 * @param name
	 * @return
	 */
	public static String getCaptionById(List<Item> list, String name) {
		Item item = getItemById(list, name);
		if ( item != null ) {
			return item.getCaption();
		}
		return "";
	}

	/**
	 * 根据name得到一个List中的Id
	 * 
	 * @param list
	 * @param name
	 * @return
	 */
	public static String getIdByCaption(List<Item> list, String name) {
		Item item = getItemByCaption(list, name);
		if ( item != null ) {
			return item.getId();
		}
		return null;
	}

	public static List<Item> getYesOrNo() {
		return yesOrNo;
	}

	public static void setYesOrNo(List<Item> yesOrNo) {
		SelectSet.yesOrNo = yesOrNo;
	}

    
    public static List<Item> getUrgentLevel() {
        return urgentLevel;
    }

    
    public static void setUrgentLevel(List<Item> urgentLevel) {
        SelectSet.urgentLevel = urgentLevel;
    }

    
    public static List<Item> getBloodType() {
        return bloodType;
    }

    
    public static void setBloodType(List<Item> bloodType) {
        SelectSet.bloodType = bloodType;
    }

    
    public static List<Item> getFinishLevel() {
        return finishLevel;
    }

    
    public static void setFinishLevel(List<Item> finishLevel) {
        SelectSet.finishLevel = finishLevel;
    }

    
    public static List<Item> getSex() {
        return sex;
    }

    
    public static void setSex(List<Item> sex) {
        SelectSet.sex = sex;
    }

    public static List<Item> getWorkrecordType() {
        return workrecordType;
    }

    public static void setWorkrecordType(List<Item> workrecordType) {
        SelectSet.workrecordType = workrecordType;
    }
	
}